#!/usr/bin/env python3
import socket
import signal
import sys
from datetime import datetime
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def sigint_handler(signum, frame):
    print ('CTRL+C received, closing socket and exiting...')
    serverSocket.shutdown(1)
    serverSocket.close()
    sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
def Main():
    if(len(sys.argv) != 2):
        print('Incorrect syntax, correct syntax is ' + sys.argv[0] + ' port.')
        sys.exit(1)
    port = sys.argv[1]
    serverSocket.bind(('',int(port)))
    while True:
        serverSocket.listen(1)
        conn, addr = serverSocket.accept()
        print ("Host " + str(addr) + "has connected")
        message="Date and time are " + str(datetime.now())
        conn.send(message.encode())
        print ("Message sent, closing connection with host " + str(addr) + ".")
        conn.close()
if __name__ == '__main__':
    Main()
